A process related to sorting is merging. By two-way merging we mean combining two sorted arrays into one sorted array. By repeatedly applying the merging procedure, we sort an array.
Example The steps done sorting with Mergesort
Mergesort Algorithm
Algorithm Design
Divide the array into two subarrays each with n/2 items.
Conquer(solve) each subarray by sorting it. Unless the array is sufficiently small, use recursion to do this.
Combine the solutions to the subarrays by merging them into a single sorted array.
Pseudo Code
voidmergesort2(index low, index high) { index mid;
voidmerge2(index low, index mid, index high) { index i, j, k; keytype U[low..high]; // A local array needed for the merging
i = low; j = mid + 1; k = low; while (i <= mid && j <= high) { if (S[i] < S[j]) { U[k] = S[i]; i++; } else { U[k] = S[j]; j++; } k++; } if (i > mid) move S[j] through S[high] to U[k] through U[high]; else move S[i] through S[mid] to U[k] through U[high]; move U[low] through U[high] to S[low] through S[high]; }
namespace algorithms { voidmergesort2(int data[ ], int low, int high); // Problem: Sort n keys in nondecreasing sequence. // Inputs: positive integer n, array of key S indexed from 1 to n. // the array S containing the keys in nondecreasing order.
voidmerge2(int data[ ], int low, int mid, int high); // Problem: Merge the two sorted subarrays of S created in Mergesort 2. // Inputs: indices low, mid, and high, and the subarray of S indexed // from low to high. The keys in array slots from low to mid are already // sorted in nondecreasing order, as are the keys in array slots from // mid +1 to high. // Outputs: the subarray of S indexed from low to high containing the // keys in nondecreasing order. } #endif
// File: sorting.cpp namespace algorithms { voidmergesort2(int data[ ], int low, int high) { int mid;
voidmerge2(int data[ ], int low, int mid, int high) { int i, j, k; // A local array needed for the merging int* temp = newint[(high-low) + 1];
i = low; j = mid + 1; k = 0; while (i <= mid && j <= high) { if (data[i] < data[j]) temp[k] = data[i++]; // Copy from first half else temp[k] = data[j++]; // Copy from second half k++; }
// Copy any remaining entries in the left and right subarrays. if (i > mid) { while (j <= high) temp[k++] = data[j++]; } else { while (i <= mid) temp[k++] = data[i++]; }
// Copy from temp back to the data array, and release temp's memory. i = low; for (k = 0; k < (high-low) + 1 ; ++k) data[i++] = temp[k];
// PROTOTYPE of a function that will test one of the sorting functions: voidtestsort(void sorter(int data[ ], int start, int last), constchar* file_name, constchar* sorting_name);
voidopen_for_read(ifstream& f, constchar filename[ ]); // Postcondition: The ifstream f has been opened for reading using the given // filename. If any errors occurred then an error message has been printed // and the program has been halted.
boolis_more(istream& source); // Precondition: input is an open istream. // Postcondition: The return value is true if input still has more data to // read; otherwise the return value is false.
voidprocess_read(ifstream& f, int data[ ]); // Precondition: input is an open istream. // Postcondition: The values in inputfile are assigned to data array.